package com.syezon.wifikey.home;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.ad.AdUtils;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.web.WebHelper;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/13.
 */

public class PopFloatAdDialog extends Dialog {


    @BindView(R.id.iv_insert_float_ad)
    ImageView mIvAd;
    @BindView(R.id.iv_close)
    ImageView mIvClose;
    private AdInfo adInfo;
    private Context context;

    public PopFloatAdDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PopFloatAdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(View.inflate(context, R.layout.app_home_dialog_pop_float_ad, null));
        ButterKnife.bind(this);
    }

    public void setAdInfo(AdInfo adInfo) {
        this.adInfo = adInfo;
    }

    @OnClick({R.id.iv_close, R.id.iv_insert_float_ad})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_insert_float_ad:
                if (adInfo != null) {
                    if (adInfo.getType() == AdInfo.AD_TYPE_DEEPLINK) {
                        Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(adInfo.getUrl()));
                        if (in.resolveActivity(getContext().getPackageManager()) == null) {
                            String[] split = adInfo.getUrl().split("://");
                            //说明系统中不存在这个activity
                            WebHelper.showAd(context, adInfo.getDesc(),"https://" + split[1], new WebHelper.SimpleWebLoadCallBack() {
                                @Override
                                public void loadComplete(String url) {
                                    super.loadComplete(url);
                                    ReportAgent.reportAdvShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                                }
                            });
                        } else {
                            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                            getContext().startActivity(in);
                            //如果想要加载成功跳转可以 这样
                        }
                    } else if (adInfo.getType() == AdInfo.AD_TYPE_WEB) {
                        WebHelper.showAd(getContext(), adInfo.getUrl(), adInfo.getName());
                    } else {
                        AdUtils.adClicked(getContext(), adInfo, null);
                    }
                }
                dismiss();
                break;
            case R.id.iv_close:
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void show() {
        super.show();
        if (adInfo != null) {
            ImageLoader.getInstance().displayImage(getContext(), adInfo.getPic(), mIvAd);
        } else {
            dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }
}
